var url = '';
var jsonData = '';

$(function() {
	loadPet();
});

function subimtAddPetBtn() { 
	var form = $("form[name=addPetform]");  
    fileUrl = ctx+'/petInfoController/addPetInfo';
 
    var options  = {
        url:fileUrl,    
        type:'post',    
        success:function(data)    
        {   
            var jsondata = eval("("+data+")");
        	if(jsondata.status==200){
        		 
        	}
        }    
    };   
    
    form.ajaxSubmit(options); 
    return false;
} 

function subimtUpdatePetBtn() { 
	var form = $("form[name=updatePetform]");  
    fileUrl = ctx+'/petInfoController/updatePetInfo';
 
    var options  = {
        url:fileUrl,    
        type:'post',    
        success:function(data)    
        {   
            var jsondata = eval("("+data+")");
        	if(jsondata.status==200){
        		 
        	}
        }    
    };   
    
    form.ajaxSubmit(options); 
    return false;
} 


function loadPet() {
	url = ctx + '/petInfoController/getPetInfo';
	jsonData = {};
	var  returnData = commonAjax();
	
	var initHtml = initManagePetHtml(returnData);
	
	$("#managePetTBody").html(initHtml);
}

function initManagePetHtml(data) {
	var returnHtml = "";
	if (data.length == 0)
		returnHtml = '<tr><td colspan="5">暂无数据</td><td></td></tr>';
	else
		$.each(data, function(index, item) {
			 
		 returnHtml +='<tr class="gradeA">'
         +'<td id="'+item.id+'">'+item.id+'</td>'
         +'<td id="'+item.pet_name+'">'+item.pet_name+'</td>'
         +'<td>'+item.age+'</td>'
         +'<td>'+item.variety+'</td>'
         +'<td>'+item.character+'</td>'
         +'<td>'+item.sex+'</td>'
         +'<td>'+item.price+'</td>'
         +'<td><a href="#" onclick="initModel('+item.id+');" data-toggle="modal" data-target="#updatePet">修改</a></td>'
         +'<td><a onclick="deletePet('+item.id+');">删除</a></td>'
         /*+'<td><a onclick="showPet('+item.id+');">查看</a></td>'*/
         +'</tr>';
		});
	
		return returnHtml;
}

function deletePet(id) {
	url = ctx + '/petInfoController/deletePetInfo';
	jsonData = {petId:id};
	var  returnData = commonAjax();
}

function initModel(id) {
	url = ctx + '/petInfoController/getSinglePetInfo';
	jsonData = {petId:id};
	var  returnData = commonAjax();
	 
	$("#petId").val(id);
	$("#petName").val(returnData[0].pet_name);
	$("#petNumber").val(returnData[0].pet_number);
	$("#pic").val(returnData[0].pic);
	$("#sex").val(returnData[0].sex);
	$("#age").val(returnData[0].age);
	$("#size").val(returnData[0].size);
	$("#variety").val(returnData[0].variety);
	$("#character").val(returnData[0].character);
	$("#price").val(returnData[0].price);
	
}
 

function commonAjax(){
	var result = '';
 	$.ajax({
		type : "GET",
		url: url,
	 	data : jsonData,
	 	async: false,
		dataType : "json",
		success: function(data) {
				result = data;
			}
		});
 	//console.info("result = "+result);
 	return result;
}